package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.Comment;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;
import com.wholesale.utils.GoodsCommentListAdapter;

public class MyCommentsLayout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private UserComments commentAsy;
	private RelativeLayout loadingLayout;
	private GoodsCommentListAdapter goodsCommentListAdapter ;
	public MyCommentsLayout(Context context, final ViewFlipper viewFlipper) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.common, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Back");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("My Comments");
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);
		goodsCommentListAdapter = new GoodsCommentListAdapter(context, bodyList, 0);
		bodyList.setAdapter(goodsCommentListAdapter);
		bodyList.setSelector(R.drawable.comments_list_bg);
		bodyList.setOnItemClickListener(moreClieckListener);
		commentAsy = new UserComments(context);
		commentAsy.execute(false);
	}

	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
		}
	};

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		default:
			break;
		}

	}

	class UserComments extends AsyncTask<Boolean, Integer, ArrayList<Comment>> {
		ProgressDialog pdialog;

		public UserComments(Context context) {
			// pdialog = new ProgressDialog(context);
			// pdialog.setTitle("Loading...");
			// pdialog.setMessage("Please wait...");
			// pdialog.setCancelable(true);
			// pdialog.setButton("cancel", new DialogInterface.OnClickListener()
			// {
			// public void onClick(DialogInterface dialog, int i) {
			// dialog.cancel();
			// UserComments.this.cancel(true);
			// }
			// });
			// pdialog.show();
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected ArrayList<Comment> doInBackground(Boolean... params) {
			ArrayList<Comment> comment = new ArrayList<Comment>();
			comment = WebServiceClient.getUserComments(Constants.loginResponse
					.getUserId(), Constants.loginResponse.getUserToken());
			if (comment == null) {
				return new ArrayList<Comment>();
			} else {
				return comment;
			}
		}

		@Override
		protected void onPostExecute(ArrayList<Comment> result) {
			goodsCommentListAdapter.addItems(result);
			goodsCommentListAdapter.notifyDataSetChanged();
			
//			List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
//			HashMap<String, Object> map1 = null;
//			for (int i = 0; i < result.size(); i++) {
//				map1 = new HashMap<String, Object>();
//				map1.put("title", result.get(i).getEmail());
//				map1.put("date", result.get(i).getAddTime().toString());
//				map1.put("comment", result.get(i).getContent());
//				data.add(map1);
//			}
//			SimpleAdapter listItemAdapter = new SimpleAdapter(context, data,
//					R.layout.commentsitem, new String[] { "title", "date",
//							"comment" }, new int[] { R.id.from_text,
//							R.id.senddate_text, R.id.comments_text });
//			bodyList.setAdapter(listItemAdapter);
			// pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

}
